    
 //
//  AboutMovieViewController.m
//  ActiveDeck
//
//  Created by Michael Cohen on 10/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomMovieViewController.h"

#define degreesToRadian(x) (M_PI * (x) / 180.0)

@implementation CustomMovieViewController

@synthesize mp, isStopped, shouldRemoveOnFinish, _orientation, _cmvcDelegate, autoplay;

-(void)readyPlayer:(NSString *)videoPath :(CGRect)videoFrame :(NSString*)orientation :(BOOL)removeOnFinish :(BOOL)isKioskEnabled{
    

    isStopped = NO;
    shouldRemoveOnFinish = removeOnFinish;
    
    _orientation = orientation;
    
    [self setFrame:videoFrame];
    
    mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]];
    [[mp view] setFrame:CGRectMake(0, 0, videoFrame.size.width, videoFrame.size.height)];
    
    [[mp view] setUserInteractionEnabled:YES];
    
    [self setBackgroundColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0]]; 
    
    if ([mp respondsToSelector:@selector(loadState)]) {
        
        if(isKioskEnabled){
            [mp setControlStyle:MPMovieControlStyleNone];
            [mp setRepeatMode:MPMovieRepeatModeOne];
        } else {
            [mp setControlStyle:MPMovieControlStyleEmbedded];
            [mp setRepeatMode:MPMovieRepeatModeNone];
        }
        
        [mp setFullscreen:NO];
        
        [[mp view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
        [mp prepareToPlay];
        [mp setScalingMode:MPMovieScalingModeAspectFit];
        
        
        
        [self addSubview:mp.view];
        
        if (autoplay) {
            [mp play];
        }

    }
    
    // Register to receive a notification when the movie has finished playing.    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil];    
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieWillEnterFullscreen:) 
                                                 name:MPMoviePlayerWillEnterFullscreenNotification 
                                               object:nil];

    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieWillExitFullscreen:) 
                                                 name:MPMoviePlayerWillExitFullscreenNotification 
                                               object:nil];   
    
    
    
}



- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{    
    isStopped = YES;
    
    if(shouldRemoveOnFinish){
        [self removeFromSuperview];
    }
}

- (void) movieWillEnterFullscreen:(NSNotification*)notification
{
    [_cmvcDelegate rotateForMovie:YES];  
}

- (void) movieWillExitFullscreen:(NSNotification*)notification
{
    [_cmvcDelegate rotateForMovie:NO];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return NO;
}

-(void)dealloc{
    // Remove observer
    [[NSNotificationCenter 	defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification 
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerWillExitFullscreenNotification object:nil];

    [super dealloc];
}
@end
